package org.testis.testdriver.resultsmatrix;

import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author barisergun
 */
public class ThreeDResultsMatrix {

    private Vector<Vector<Vector<Boolean>>> m_Case_3d_Results;

    public ThreeDResultsMatrix() {
        m_Case_3d_Results = new Vector<Vector<Vector<Boolean>>>();

    }

    public void setResultIn3dArray(int invokeIndex,
            int childIndex,
            int childDataIndex,
            Boolean result) {
        boolean update = true;
        try {
            this.getResultFrom3dArray(invokeIndex,
                    childIndex,
                    childDataIndex); // I check if result
        } catch (IndexOutOfBoundsException ee) {
            update = false;
        }

        resultIn3dArray(invokeIndex,
                childIndex,
                childDataIndex,
                result,
                update);
    }

    private void resultIn3dArray(int invokeIndex,
            int childIndex,
            int childDataIndex,
            Boolean result,
            Boolean update) {

        Vector<Vector<Boolean>> innerVector = null;

        try {
            innerVector = this.m_Case_3d_Results.get(
                    invokeIndex);
        } catch (ArrayIndexOutOfBoundsException eee) {
        }
        if (innerVector != null) {

            Vector<Boolean> innerInnerVector = null;
            try {
                innerInnerVector = innerVector.get(childIndex);
            } catch (ArrayIndexOutOfBoundsException eee) {
            }

            if (innerInnerVector != null) {

                setElement(innerInnerVector,
                        childDataIndex,
                        result,
                        update);

            } else {
                Vector<Boolean> newInInVector = new Vector<Boolean>();
                newInInVector.add(result); // as it is first creation do not
                // need to give childDataIndex

                setElement(innerVector,
                        childIndex,
                        newInInVector,
                        update);

            }
        } else {
            Vector<Vector<Boolean>> newInVector = new Vector<Vector<Boolean>>();
            Vector<Boolean> newInInVector = new Vector<Boolean>();

            newInInVector.add(result);// as it is first creation do not
            // need to give childDataIndex

            setElement(newInVector,
                    childIndex,
                    newInInVector,
                    update);
            // as it is first creation do not
            // need to give childIndex

            setElement(this.m_Case_3d_Results,
                    invokeIndex,
                    newInVector,
                    update);


        }

    }

    private void setElement(Vector toBeProcesses,
            int index,
            Object result,
            Boolean update) {
        if (update) {
            toBeProcesses.set(index,
                    result);
        } else {
            toBeProcesses.add(index,
                    result);
        }

    }

    public Vector<Vector<Vector<Boolean>>> get3dArray() {
        return this.m_Case_3d_Results;
    }

    public Boolean getResultFrom3dArray(int invokeIndex,
            int childIndex,
            int childDataIndex) {

        return this.m_Case_3d_Results.get(invokeIndex).get(childIndex).get(
                childDataIndex);

    }

    public void resetVectors() {
        for (Iterator<Vector<Vector<Boolean>>> it = m_Case_3d_Results.iterator(); it.hasNext();) {

            Vector<Vector<Boolean>> innerVector = it.next();
            for (Iterator<Vector<Boolean>> it2 = innerVector.iterator(); it2.hasNext();) {
                it2.next().removeAllElements();
            }
            innerVector.removeAllElements();
        }
        m_Case_3d_Results.removeAllElements();
    }
}
